package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdHouseData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdWildDataDaoImpl extends TempDataObjectDao<StrongholdHouseData> {
    public static String getStrongholdHouseDataSql = "select * from t_c_activity_stronghold_wild where activityId=? and groupId=?";

    public Map<Integer, StrongholdHouseData> getStrongholdHouseData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdHouseData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getStrongholdHouseDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdHouseData strongholdHouseData = parse(rs);
                dataMap.put(strongholdHouseData.getHouseId(), strongholdHouseData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdHouseDataSql = "update t_c_activity_stronghold_wild set refreshBoxTime=? where activityId=? and groupId=? and houseId=?";
    @Override
    protected boolean updateData(StrongholdHouseData strongholdHouseData) {
        strongholdHouseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, strongholdHouseData.getRefreshBoxTime()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdHouseData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdHouseData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdHouseData.getHouseId()));
        if (getDb().executeNoneQuery(updateStrongholdHouseDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdHouseDataSql = "insert into t_c_activity_stronghold_wild(activityId,groupId,houseId,refreshBoxTime) values (?,?,?,?)";
    @Override
    protected boolean addData(StrongholdHouseData strongholdHouseData) {
        strongholdHouseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdHouseData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdHouseData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdHouseData.getHouseId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdHouseData.getRefreshBoxTime()));
        if (getDb().executeNoneQuery(insertStrongholdHouseDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdHouseData parseData(ResultSet rs) throws SQLException {StrongholdHouseData strongholdHouseData = new StrongholdHouseData();
        strongholdHouseData.setActivityId(rs.getInt("activityId"));
        strongholdHouseData.setGroupId(rs.getInt("groupId"));
        strongholdHouseData.setHouseId(rs.getInt("houseId"));
        strongholdHouseData.setRefreshBoxTime(rs.getLong("refreshBoxTime"));
        return strongholdHouseData;
    }





}
